/**
 * 
 */
package com.jie.eclipse.pan.gef.dialog;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.action.ToolBarManager;
import org.eclipse.jface.viewers.CellEditor;
import org.eclipse.jface.viewers.CheckboxTableViewer;
import org.eclipse.jface.viewers.ColumnWeightData;
import org.eclipse.jface.viewers.TableLayout;
import org.eclipse.jface.viewers.TextCellEditor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.custom.ViewForm;
import org.eclipse.swt.dnd.DND;
import org.eclipse.swt.dnd.Transfer;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.TabFolder;
import org.eclipse.swt.widgets.TabItem;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.ui.forms.events.ExpansionAdapter;
import org.eclipse.ui.forms.events.ExpansionEvent;
import org.eclipse.ui.forms.widgets.ExpandableComposite;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipse.wst.common.ui.internal.dnd.ObjectTransfer;
import org.eclipse.wst.common.ui.internal.dnd.ViewerDragAdapter;

import com.jie.eclipse.pan.bind.BaseDataBind;
import com.jie.eclipse.pan.dao.QMDao;
import com.jie.eclipse.pan.data.BaseDataEntity;
import com.jie.eclipse.pan.form.item.table.Tbody;
import com.jie.eclipse.pan.form.item.table.TbodyTd;
import com.jie.eclipse.pan.form.item.table.TbodyTr;
import com.jie.eclipse.pan.form.item.table.Thead;
import com.jie.eclipse.pan.form.item.table.TheadTd;
import com.jie.eclipse.pan.form.item.table.TheadTr;
import com.jie.eclipse.pan.form.support.FormItem;
import com.jie.eclipse.pan.gef.action.SeachActionGroup;
import com.jie.eclipse.pan.gef.action.SelectedColumnsActionGroup;
import com.jie.eclipse.pan.gef.listener.PanTableOrderDropAdapter;
import com.jie.eclipse.pan.listener.PMouseListener;
import com.jie.eclipse.pan.listener.PSelectionListener;
import com.jie.eclipse.pan.swtitem.PCombo;
import com.jie.eclipse.pan.swtitem.table.PTable;
import com.jie.eclipse.pan.swtitem.table.PTableColumn;
import com.jie.eclipse.pan.swtitem.table.PTableViewer;
import com.jie.eclipse.pan.swtitem.table.PllTableContentProvider;
import com.jie.eclipse.pan.swtitem.table.PllTableLabelProvider;
import com.jie.eclipse.pan.swtitem.table.cellmodifier.SelectColumnsTableCellModifier;
import com.jie.eclipse.pan.swtitem.widgets.PanStringComboBoxCellEditor;
import com.jie.eclipse.pan.utils.ObjectHelper;
import com.jie.eclipse.pan.utils.StringBuilderEx;
import com.jie.eclipse.pan.utils.StringHelper;
import com.jie.eclipse.pan.utils.Utils;

/**
 * @author yanjie
 *
 */
public class TableDialog extends BaseDialog{
	private TabFolder tabFolder = null;

	/**
	 * @param parentShell
	 * @param data
	 * @param action
	 */
	public TableDialog(Shell parentShell, BaseDataEntity data, String action) {
		super(parentShell, data, action);
	}
	
	@Override
	protected Point getInitialSize() {
		// Point point = super.getInitialSize();
		// point.x = 600;
		return new Point(600, 600);
	}

	
	@Override
	protected Control createDialogArea(Composite parent) {
		Color bgcolor = Display.getDefault().getSystemColor(SWT.COLOR_WIDGET_BACKGROUND);
		
		ScrolledComposite  form = new ScrolledComposite  (parent, SWT.BORDER | SWT.V_SCROLL);
		form.setLayout(new GridLayout(1, false));
		form.setLayoutData(new GridData(GridData.FILL_BOTH));
		form.setAlwaysShowScrollBars(true);  
		form.setExpandHorizontal(true);
		form.setExpandVertical(true);
		form.setMinHeight(420);  
		
		Composite composite = new Composite (form, SWT.NONE);
		composite.setLayout(new GridLayout(8, false));
		composite.setLayoutData(new GridData(GridData.FILL_BOTH));
		composite.setBackground(bgcolor);
		Label lb = null;
		GridData gd = null;
		Group group = null;
		
		lb=  new Label(composite,SWT.RIGHT);
		lb.setText("ID:");
		lb.setBackground(bgcolor);
		Text id = new Text(composite, SWT.BORDER);
		id.setLayoutData(new GridData(60, 15));
		id.setText("grid1");
		this.getDataBind().bind("id", id);	
		

		lb=  new Label(composite,SWT.RIGHT);
		lb.setText("数据源:");
		lb.setBackground(bgcolor);
		Text dataSet = new Text(composite, SWT.BORDER);
		dataSet.setLayoutData(new GridData(60, 15));
		dataSet.setText("default");
		dataSet.setEnabled(false);
		this.getDataBind().bind("dataSet", dataSet);
		
		Button defaultDataSet = new Button(composite,SWT.CHECK);
		defaultDataSet.setText("默认数据源");
		defaultDataSet.setBackground(bgcolor);
		defaultDataSet.setData(true);
		defaultDataSet.setData("dataSet", dataSet);
		defaultDataSet.setSelection(true);
		defaultDataSet.addSelectionListener(new PSelectionListener(null){
			@Override
			public void widgetSelected(SelectionEvent event) {
				Button btn1 = (Button)event.getSource();
				Text dataSet = (Text)btn1.getData("dataSet");
				if(btn1.getSelection()){
					dataSet.setText("default");
					dataSet.setEditable(false);
					dataSet.setEnabled(false);
				}else{
					dataSet.setText("");
					dataSet.setEditable(true);
					dataSet.setEnabled(true);
				}
			}
		});
		this.getDataBind().bind("defaultDataSet", defaultDataSet);
		
		
		Button loopFisrt = new Button(composite,SWT.CHECK);
		loopFisrt.setText("空值循环一次");
		loopFisrt.setBackground(bgcolor);
		loopFisrt.setData(true);
		this.getDataBind().bind("loopFisrt", loopFisrt);
		
		Button view = new Button(composite, SWT.CHECK);
		view.setText("锁表头");
		view.setBackground(bgcolor);
		view.setData("fixedRow");
		this.getDataBind().bind("view", view);
		
		Button toolbar =  new Button(composite, SWT.CHECK);
		toolbar.setText("显示默认工具栏");
		toolbar.setBackground(bgcolor);
		toolbar.setData(true);
		this.getDataBind().bind("toolbar", toolbar);
		
		group = new Group(composite, SWT.NULL);
		group.setText("表格相关配置");
		FillLayout layout = new FillLayout();
		group.setLayout(layout);
		gd = new GridData(GridData.FILL_BOTH);
		gd.horizontalSpan=8;
		group.setLayoutData(gd);
		
		tabFolder = new TabFolder(group, SWT.NONE);
		createTableConfigTab(this,tabFolder,"tableConfig","表格配置");
		
		this.reloadRecord();
	    form.setContent(composite);
		return parent;
	}
	
	public void createTableConfigTab(BaseDialog dialog, TabFolder tabFolder, String tabid,
			String tabname) {
		Color bgcolor = Display.getDefault().getSystemColor(SWT.COLOR_WIDGET_BACKGROUND);
		GridData gd = null;
		BaseDataBind tabbind = new BaseDataBind();
		this.getDataBind().bind(tabid, tabbind);
		
		TabItem tabItme1 = new TabItem(tabFolder, SWT.NONE);
		tabItme1.setData("id", tabid);
		tabItme1.setText(tabname);

		Composite tabcp = new Composite(tabFolder, SWT.NONE);
		tabcp.setLayout(new GridLayout(1, false));
		gd = new GridData(GridData.FILL_BOTH);
		tabcp.setLayoutData(gd);
		tabcp.setBackground(bgcolor);
		
//		Composite content = new Composite(tabcp, SWT.NONE);
//		content.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
//		content.setLayout(new GridLayout(2, false));
//		
//		Label lb=  new Label(content,SWT.RIGHT);
//		lb.setText("列数(colModel):");
//		Text colModel = new Text(content, SWT.BORDER);
//		colModel.setLayoutData(new GridData(200, 15));
//		tabbind.bind("colModel", colModel);

		//创建数据源区域的面板
		createDataSourceComposite(this,tabbind, tabcp, tabid);
		
		// 创建表单属性的表格信息面板
		createDateGridComposite(this,tabbind, tabcp, tabid);

		tabItme1.setControl(tabcp);
	}
	
	/**
	 * @param gridComposite
	 * @param cs
	 *            创建数据源区域的面板
	 */
	public void createDataSourceComposite(BaseDialog dialog, BaseDataBind tabbind,Composite tabcp,
			String tabid) {
		FormToolkit toolkit = new FormToolkit(tabcp.getDisplay());
		Color bgcolor = Display.getDefault().getSystemColor(SWT.COLOR_WIDGET_BACKGROUND);
		ExpandableComposite ec = toolkit.createExpandableComposite(tabcp,
				ExpandableComposite.TREE_NODE
						| ExpandableComposite.NO_TITLE_FOCUS_BOX);
		ec.setBackground(bgcolor);
		ec.setData("dialog", dialog);
		ec.setText("根据SQL带出表單属性栏位");
		GridData gd = new GridData(GridData.FILL_HORIZONTAL);
//		gd.horizontalSpan=2;
		ec.setLayoutData(gd);
		ec.addExpansionListener(new ExpansionAdapter() {
			public void expansionStateChanged(ExpansionEvent e) {
				// 根据部件的新尺寸重新定位和更新滚动条
				Composite ec = (ExpandableComposite) e.getSource();
				ec.layout();

				BaseDialog dialog = (BaseDialog) ec.getData("dialog");
				dialog.getShell().pack(true);
				dialog.getShell().layout();
			}
		});

		Composite group1 = toolkit.createComposite(ec, SWT.NONE);
		group1.setLayout(new GridLayout(3, false));
		group1.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		ec.setClient(group1);

		Label labe11 = new Label(group1, SWT.RIGHT);
		labe11.setText("DataSource:");
		PCombo combo1 = new PCombo(group1, SWT.READ_ONLY);
		String[] datasources = QMDao.getPanAllDataSource(false);
		combo1.setItems(datasources);
		
		combo1.setText(this.getQmDao().getDefaultDataSource());
		combo1.setLayoutData(new GridData(100, 24));
		for (int i = 0; i < datasources.length; i++) {
			combo1.setData(String.valueOf(i), datasources[i]);
		}
		
		this.getDataBind().bind("dataSource", combo1);

		Button button1 = new Button(group1, SWT.NONE);
		button1.setText("语句分析");

		Label labe12 = new Label(group1, SWT.RIGHT);
		labe12.setText("SQl:");
		Text text2 = new Text(group1, SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL);
		GridData text2GridData = new GridData(GridData.FILL_HORIZONTAL);
		text2GridData.horizontalSpan = 2;
		text2GridData.heightHint = 50;
		text2.setLayoutData(text2GridData);
		text2.setText(this.getQmDao().getDefaultSQL());
		this.getDataBind().bind("sql", text2);

		Label labe13 = new Label(group1, SWT.RIGHT);
		labe13.setText("可选栏位:");

		ViewForm vform = new ViewForm(group1, SWT.NONE);
		// vform.setLayout(new FillLayout());
		vform.setLayout(new GridLayout());
		GridData vformData = new GridData(GridData.FILL_HORIZONTAL);
		vformData.horizontalSpan = 2;
		vformData.heightHint = 150;
		vform.setLayoutData(vformData);

		PTableViewer tv1 = createTableViewer1(vform);
		CheckboxTableViewer ctv1 = new CheckboxTableViewer(tv1.getTable());
		BaseDataEntity data1 = this.getDataInstance();
		data1.spov(tabid + "tv1", tv1);
		data1.spov(tabid + "ctv1", ctv1);

		tv1.setContentProvider(new PllTableContentProvider()); // 内容器
		tv1.setLabelProvider(new PllTableLabelProvider(tv1));// 标签器

		SeachActionGroup actionGroup1 = new SeachActionGroup(this,tabid,"TableDialog");
		actionGroup1.fillContextMenu(new MenuManager());
		ToolBar toolbar1 = new ToolBar(vform, SWT.FLAT | SWT.RIGHT);
		toolbar1.setBackground(bgcolor);
		ToolBarManager toolbarManager = new ToolBarManager(toolbar1);
		actionGroup1.fillActionToolbars(toolbarManager);

		vform.setContent(tv1.getControl());
		vform.setTopLeft(toolbar1);

		BaseDataEntity data2 = new BaseDataEntity();
		data2.spov("tv", tv1);
		data2.spov("bind", this.getDataBind());

		button1.addMouseListener(new PMouseListener(data2) {
			public void mouseUp(MouseEvent paramMouseEvent) {
				QMDao qmDao = QMDao.getInstance();
				BaseDataBind bind = (BaseDataBind) this.gpov("bind");
				PTableViewer tv1 = (PTableViewer) this.gpov("tv");
				tv1.setInput(qmDao.getColumnsInfo(bind.getValue("dataSource"),
						bind.getValue("sql")));
			}
		});
	}
	
	/**
	 * @param group1
	 * @return 可选栏位的表格查看器
	 */
	public PTableViewer createTableViewer1(ViewForm vform) {
		PTableViewer tv1 = new PTableViewer(new PTable(vform, SWT.CHECK
				| SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER
				| SWT.FULL_SELECTION));
		PTable table = tv1.getPTable();
		table.setLayout(new GridLayout());
		GridData tableGridData = new GridData(GridData.FILL_BOTH);
		// tableGridData.horizontalSpan=2;
		// tableGridData.heightHint = 100;
		table.setLayoutData(tableGridData);
		table.setHeaderVisible(true); // 表头显示
		table.setLinesVisible(true); // 表格线显示

		TableLayout tLayout = new TableLayout();
		table.setLayout(tLayout);

		tLayout.addColumnData(new ColumnWeightData(20));
		PTableColumn col1 = new PTableColumn(table, SWT.NONE);
		col1.setText("名称");
		col1.setName("cid");

		tLayout.addColumnData(new ColumnWeightData(30));
		PTableColumn col2 = new PTableColumn(table, SWT.NONE);
		col2.setText("标题");
		col2.setName("caption");
		
		tv1.addDragSupport(DND.DROP_MOVE,new Transfer[] { ObjectTransfer.getInstance() },new ViewerDragAdapter(tv1));
		tv1.addDropSupport(DND.DROP_MOVE,new Transfer[] { ObjectTransfer.getInstance() }, new PanTableOrderDropAdapter(tv1));

		table.reloadColumns();
		return tv1;
	}
	
	/**
	 * @param gridComposite
	 * @param cs
	 *            创建表格信息面板
	 */
	public void createDateGridComposite(BaseDialog dialog,BaseDataBind tabbind, Composite tabcp,
			String tabid) {
		Group group1 = new Group(tabcp, SWT.NULL);
		group1.setText("已选栏位");
		group1.setLayout(new GridLayout(2, false));
		GridData gd = new GridData(GridData.FILL_HORIZONTAL);
//		gd.horizontalSpan=2;
		group1.setLayoutData(gd);

		ViewForm vform = new ViewForm(group1, SWT.NONE);
		// vform.setLayout(new FillLayout());
		vform.setLayout(new GridLayout());
		GridData vformData = new GridData(GridData.FILL_HORIZONTAL);
		vformData.horizontalSpan = 2;
		vformData.heightHint = 300;
		vform.setLayoutData(vformData);

		PTableViewer tv2 = createTableViewer2(vform);
		CheckboxTableViewer ctv2 = new CheckboxTableViewer(tv2.getTable());
		BaseDataEntity data1 = this.getDataInstance();
		data1.spov(tabid + "tv2", tv2);
		data1.spov(tabid + "ctv2", ctv2);

		tv2.setContentProvider(new PllTableContentProvider()); // 内容器
		tv2.setLabelProvider(new PllTableLabelProvider(tv2));// 标签器

		// XMLNode node = detailPage.getCurNode();
		// tv2.setInput(node.getChildBaseDataEntities());

		SelectedColumnsActionGroup actionGroup1 = new SelectedColumnsActionGroup(this,tabid,"TableDialog");
		actionGroup1.fillContextMenu(new MenuManager());
		ToolBar toolbar1 = new ToolBar(vform, SWT.FLAT | SWT.RIGHT);
		Color bgcolor = Display.getDefault().getSystemColor(
				SWT.COLOR_WIDGET_BACKGROUND);
		toolbar1.setBackground(bgcolor);
		ToolBarManager toolbarManager = new ToolBarManager(toolbar1);
		actionGroup1.fillActionToolbars(toolbarManager,true);

		vform.setContent(tv2.getControl());
		vform.setTopLeft(toolbar1);
	}

	/**
	 * @param group1
	 * @return 已选栏位的表格查看器
	 */
	public PTableViewer createTableViewer2(ViewForm vform) {
		PTableViewer tv2 = new PTableViewer(new PTable(vform, SWT.CHECK
				| SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER
				| SWT.FULL_SELECTION));
		PTable table = tv2.getPTable();
//		TableCursor cursor = new TableCursor(table, SWT.NONE);
//		cursor.addMouseListener(new PMouseListener() {
//			@Override
//			public void mouseDoubleClick(MouseEvent e) {
//				Utils.print(e.getSource().getClass().getName());
//			}
//		});
		
		table.setLayout(new GridLayout());
		GridData tableGridData = new GridData(GridData.FILL_BOTH);
		table.setLayoutData(tableGridData);
		table.setHeaderVisible(true);//表头显示
		table.setLinesVisible(true);//表格线显示

		PTableColumn col1 = new PTableColumn(table, SWT.NONE);
		col1.setText("名称");
		col1.setName("name");
		col1.setWidth(80);

		PTableColumn col2 = new PTableColumn(table, SWT.NONE);
		col2.setText("标题");
		col2.setName("caption");
		col2.setWidth(80);

		PTableColumn col3 = new PTableColumn(table, SWT.NONE);
		col3.setText("宽度");
		col3.setName("width");
		col3.setWidth(40);
		
		PTableColumn col4 = new PTableColumn(table, SWT.NONE);
		col4.setText("控件类型");
		col4.setName("xtype_text");
		col4.setWidth(70);
		
		PTableColumn col5 = new PTableColumn(table, SWT.NONE);
		col5.setText("編輯類型");
		col5.setName("editortype_text");
		col5.setWidth(70);
		
//		PTableColumn col6 = new PTableColumn(table, SWT.NONE);
//		col6.setText("验证");
//		col6.setName("valid");
//		col6.setWidth(60);
		
		PTableColumn col7 = new PTableColumn(table, SWT.NONE);
		col7.setText("明細");
		col7.setName("detail");
		col7.setWidth(80);
		
		table.reloadColumns();
		tv2.setColumnProperties(new String[] { "name", "caption", "width","xtype_text","editortype_text","detail" });
		CellEditor[] editors = new CellEditor[5];
		editors[0] = new TextCellEditor(table, SWT.NONE);
		editors[1] = new TextCellEditor(table, SWT.NONE);
		editors[2] = new TextCellEditor(table, SWT.NONE);
		
		Map<String ,String> editorMap = QMDao.getInstance().getEditorMap(true);
		PanStringComboBoxCellEditor combo1 = new PanStringComboBoxCellEditor(table,editorMap.keySet().toArray(new String[]{}), SWT.READ_ONLY);
		Iterator it = editorMap.keySet().iterator();
		while(it.hasNext()){
			String key = (String)it.next();
			combo1.setData(key, editorMap.get(key));
		}
		BaseDataEntity tempData = new BaseDataEntity();
		tempData.spov("combo1", combo1);
		tempData.spov("col5", col5);
		editors[3]=combo1;
		
		PanStringComboBoxCellEditor combo2 = new PanStringComboBoxCellEditor(table,new String[] {"--","静态","动态" }, SWT.READ_ONLY);
		combo2.setData("--", "");
		combo2.setData("静态", "static");
		combo2.setData("动态", "dynamic");
		editors[4]=combo2;
		
		List<String> list = new ArrayList<String>();
		list.add("name");
		list.add("caption");
		list.add("width");
		list.add("xtype_text");
		list.add("editortype_text");
		
		tv2.setCellEditors(editors);
		tv2.setCellModifier(new SelectColumnsTableCellModifier(tv2,list));

		tv2.addDragSupport(DND.DROP_MOVE,new Transfer[] { ObjectTransfer.getInstance() },new ViewerDragAdapter(tv2));
		tv2.addDropSupport(DND.DROP_MOVE,new Transfer[] { ObjectTransfer.getInstance() }, new PanTableOrderDropAdapter(tv2));
		
		BaseDataEntity data = new BaseDataEntity();
		data.spov("tv2", tv2);
		tv2.getTable().addMouseListener(new PMouseListener(data){
			@Override
			public void mouseDoubleClick(MouseEvent e) {
				PTableViewer tv2 = (PTableViewer)this.getData().gpov("tv2");
				PTable table = tv2.getPTable();
				int rowIndex = table.getSelectionIndex();
				int columnIndex = -1;
				int left = 0;
				for (int i = 0; i < table.getColumns().length; i++) {
					left += table.getColumns()[i].getWidth();
					if (e.x < left) {
						columnIndex = i;
						break;
					}
				}
				if (columnIndex == 5) {
					TableItem tabitem = table.getItem(rowIndex);
					BaseDataEntity data = (BaseDataEntity)tabitem.getData();
					if(data==null)return;
					BaseDialog dialog = getDialog(data);
					if (dialog != null) {
						//初始化控件 的值
						BaseDataEntity defaultData = dialog.getDefaultData();
						if(defaultData == null){
							defaultData = new BaseDataEntity();
							dialog.setDefaultData(defaultData);
						}
						
						data.CopyTo(defaultData, false);
						if(dialog.open() == BaseDialog.OK){
							table.ReloadTableItemRecord(tabitem, data);
						}
		            }
				}
			}
		});
		return tv2;
	}
	
	@Override
	public void initRootItem(BaseDataEntity data,BaseDataBind bind,StringBuilderEx code) {
		if(bind == null)return ;
		BaseDataEntity record = bind.getRecord();
		FormItem parent = new FormItem();
		
		
		FormItem columnEditors = new FormItem();
		columnEditors.setNodeName("jm:columnEditors");
		
		FormItem table = getNewFormItem();//表
		if (table == null)return;
		table.configItem(record);
		
		TabFolder tabFolder = getTabFolder();
		QMDao dao = QMDao.getInstance();
		for (int i = 0; i < tabFolder.getItems().length; i++) {
			TabItem tabItem = tabFolder.getItem(i);
			String tabid = (String)tabItem.getData("id");
			if("tableConfig".equalsIgnoreCase(tabid)){
				//表格配置
				BaseDataEntity tabRecord = (BaseDataEntity)record.gpov(tabid);
				CheckboxTableViewer ctv2 = (CheckboxTableViewer)this.getDataInstance().gpov(tabid+"ctv2");
				Thead thead = new Thead();
				Tbody tbody = new Tbody();
				
				TheadTr tr1 = new TheadTr();
				TbodyTr tr2 = new TbodyTr();
				
				for (int j = 0; j < ctv2.getTable().getItems().length; j++) {
					TableItem tabitem = ctv2.getTable().getItems()[j];
					BaseDataEntity row = (BaseDataEntity)tabitem.getData();
					
					//表头
					TheadTd td1 = new TheadTd();
					td1.configItem(row);
					tr1.addItem(td1);
					
					//表身
					TbodyTd td2 = new TbodyTd();
					td2.configItem(row);
					tr2.addItem(td2);
					
					String xtype = row.gpsv("xtype");
					if(!StringHelper.IsNullOrEmpty(xtype)){
						FormItem item = getFormItem(row.gpsv("xtype"));
						
						if(item != null){
							if("static".equalsIgnoreCase(row.gpsv("editortype"))){
								item.configItem(row,"table-static");
								//靜態控件
								td2.addItem(item);
								td2.RemoveExtValue("cid");//如果有静态控件 要移除cid属性
							}else if("dynamic".equalsIgnoreCase(row.gpsv("editortype"))){
								item.configItem(row,"table-dynamic");
								FormItem columnEditor = new FormItem();
								columnEditor.setNodeName("jm:columnEditor");
								columnEditor.spsv("id", "e_"+row.gpsv("name"));
								columnEditor.spsv("type", xtype);
								columnEditor.addItem(item);
								columnEditors.addItem(columnEditor);
								
								//动态编辑控件配置
								StringBuilderEx strConfig = new StringBuilderEx();
								strConfig.Append("cid:'%1$s'",row.gpsv("name"));
								strConfig.Append(",editor:'%1$s'",xtype);
								strConfig.Append(",editorid:'e_%1$s'",row.gpsv("name"));
								td1.spsnnv("config", strConfig.toString());
							}
						}
					}
				}
				
				thead.addItem(tr1);
				tbody.addItem(tr2);
				table.addItem(thead);
				table.addItem(tbody);
			}
		}
		
		if(columnEditors.getItems() != null && columnEditors.getItems().size() >0){
			parent.addItem(columnEditors);
		}
		
		parent.addItem(table);
		this.setRoot(parent);
	}
	
	@Override
	public void fillConfig(BaseDataEntity config, BaseDataBind dataBind) {
		super.fillConfig(config, dataBind);
	}
	
	@Override
	public String getTitle() {
		return "表格設置";
	}
	
	public TabFolder getTabFolder() {
		return tabFolder;
	}
	
	public BaseDialog getDialog(BaseDataEntity row){
		String xtype = row.gpsv("xtype");
		String strClass = QMDao.getInstance().getXtypeDialogClassMap().get(xtype);
		BaseDialog dialog = null;
		if(!StringHelper.IsNullOrEmpty(strClass)){
			dialog = (BaseDialog)ObjectHelper.newInstance(strClass, new Object[]{Display.getDefault().getActiveShell(),row,"fillConfig"});
		}
		return dialog;
	}
	
	public FormItem getFormItem(String xtype){
		if(StringHelper.IsNullOrEmpty(xtype)) return null;
		String strClass = QMDao.getInstance().getXtypeFormItemClassMap().get(xtype);
		FormItem formItem = null;
		if(!StringHelper.IsNullOrEmpty(strClass)){
			formItem = (FormItem)ObjectHelper.newInstance(strClass,null);
		}
		return formItem;
	}
}
